import React, {useContext, useRef, useEffect} from 'react';
import {
  View,
  PanResponder,
  Alert,
  Platform,
  ScrollView,
  FlatList,
} from 'react-native';
import dayjs from 'dayjs';
import Toast from 'react-native-root-toast';
import {useNavigation} from '@react-navigation/native';
import {useDebounceEffect, useMount, useUnmount, useSafeState} from 'ahooks';
import {Text, Button, Input, Dialog, ListItem, Avatar} from '@rneui/themed';
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';
import {useSelector, useDispatch} from 'react-redux';
import AsyncStorage from '@react-native-async-storage/async-storage';
import GlobalContext from '@/GlobalContext';
import ImagePicker from 'react-native-image-crop-picker';
import fileExtension from 'file-extension';
import {ApiHost, OssHost} from '@/config/common';
import {setKeyGetAccountInfo} from '@/store';
import DocumentPicker from 'react-native-document-picker';

import RNFS from 'react-native-fs';
import RNFetchBlob from 'react-native-blob-util';
import {round} from 'lodash';
import {Dirs, FileSystem} from 'react-native-file-access';

import getFilesInfoSystem from '../getFilesInfoSystem';
import getFilesInfoMongoDb from '../getFilesInfoMongoDb';
export default () => {
  const {localMongoDbFileSystem} = useContext(GlobalContext);

  const [loading, setLoading] = useSafeState(false);
  // end: 2023-09-18T15:21:14+08:00 use 199512 ms total 57524
  // end: 2023-09-18T15:26:02+08:00 use 181646 ms total 52836

  return (
    <>
      <Button
        loading={loading}
        title="UpdateSDcardDir"
        onPress={async () => {
          setLoading(true);
          const start = dayjs();
          const pPath = Dirs.SDCardDir;
          const db = localMongoDbFileSystem;

          const rtList = [];
          let total = 0;
          const depthSetFiles = async pPath => {
            const systemFiles = await getFilesInfoSystem({pPath});
            systemFiles.forEach(item => {
              rtList.push(item);
            });
            const dirs = systemFiles.filter(item => item.isDir);
            // console.log(pPath, systemFiles.length);
            total += systemFiles.length;
            const end = dayjs();
            console.log(
              '  end:',
              end.format(),
              'use',
              end.diff(start, 'ms'),
              'ms',
              'total',
              total,
            );
            for (const item of new Set(dirs)) {
              depthSetFiles(item.path);
            }
          };

          depthSetFiles(pPath);

          setLoading(false);
        }}
        containerStyle={{
          margin: 20,
        }}
      />
    </>
  );
};
